package mx.itesm.minAFN.ut;

/**
 * @(#)GUI.java
 *
 *
 * @author 
 * @version 1.00 2010/11/18
 */

import java.util.ArrayList;

import mx.itesm.minAFN.AFN;
import mx.itesm.minAFN.AFNState;
import mx.itesm.minAFN.AFNStateType;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.StringTokenizer;

public class GUI extends JFrame implements ActionListener, ItemListener{
        
    /**
     * Creates a new instance of <code>GUI</code>.
     */
     
     final int STATE_INIT = 0;
     final int STATE_CREATE_GUI =1;
     int initialStates ;
     
     
     
     JLabel l_state[];
     JPanel statePanel, aPanel, bPanel,ePanel, fPanel, topPanel, mainPanel,buttonPanel; 
     
     JTextField t_text1[];
     JTextField t_text2[];
     JTextField t_text3[];
     JButton b_OK, b_restart;
     Checkbox c_final[];
     String transitionTable[];
     int currentState;
    public GUI() {
    	currentState = STATE_INIT;
    	setTitle("Automata Minimizer 3000+");
    	
    	initialStates= Integer.parseInt(JOptionPane.showInputDialog(this,"Escribe el numero de estados de tu AFN" ));
    	
    	
    	createGUI(initialStates);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	//this.setResizable(false);
    }
    
    
    public void createGUI(int numStates){
    	
    	if(mainPanel != null){
	    	removeAllComponents(mainPanel);
	    	removeAllComponents(statePanel);
	    	removeAllComponents(aPanel);
	    	removeAllComponents(bPanel);
	    	removeAllComponents(ePanel);
	    	removeAllComponents(fPanel);
	    	removeAllComponents(topPanel);
	    	removeAllComponents(buttonPanel);
	    	this.remove(topPanel);
	    	this.remove(mainPanel);
	    	this.remove(buttonPanel);
	    	
    	}
    	transitionTable = new String[numStates];
    	
    	topPanel = new JPanel();
    	statePanel= new JPanel();
    	aPanel = new JPanel();
    	bPanel = new JPanel();
    	ePanel= new JPanel();
    	fPanel= new JPanel();
    	buttonPanel = new JPanel();
    	b_OK = new JButton("MINIMIZE IT!!");
    	b_OK.addActionListener(this);
    	b_restart = new JButton("Start Over");
    	b_restart.addActionListener(this);
    	mainPanel = new JPanel();
    	setLayout(new BorderLayout());
    	add(topPanel,"North");
    
    	
    	mainPanel.setLayout(new GridLayout(1,5));
    	topPanel.setLayout(new GridLayout(1,4));
    	topPanel.add(new JLabel("     State"));
    	topPanel.add(new JLabel("    a    "));
    	topPanel.add(new JLabel("    b    "));
    	topPanel.add(new JLabel("  epsilon "));
    	topPanel.add(new JLabel(" accept"));
    	
    	statePanel.setLayout(new GridLayout(numStates,1));
    	aPanel.setLayout(new GridLayout(numStates,1));
    	bPanel.setLayout(new GridLayout(numStates,1));
    	ePanel.setLayout(new GridLayout(numStates,1));
    	fPanel.setLayout(new GridLayout(numStates,1));
    	
    	c_final= new Checkbox[numStates];
    	l_state= new JLabel[numStates];
    	t_text1= new JTextField[numStates];
    	t_text2= new JTextField[numStates];
    	t_text3= new JTextField[numStates];
    	for(int i=0; i< numStates;i++){
    		if(i==0)
    			l_state[i]= new JLabel("        >>q"+i);
    		else	
    		    l_state[i]= new JLabel("          q"+i);
    		t_text1[i]= new JTextField(8);
    		t_text2[i]= new JTextField(8);
    		t_text3[i]= new JTextField(8);
    		c_final[i] = new Checkbox();
    		c_final[i].addItemListener(this);
    		//System.out.println("creating and adding Label");
    		statePanel.add(l_state[i]);
    		aPanel.add(t_text1[i]);
    		bPanel.add(t_text2[i]);
    		ePanel.add(t_text3[i]);
    		fPanel.add(c_final[i]);
    		
    		//transitionTable[i]="q"+i+" ";
    		
    		
    	}
    	
    	
    	mainPanel.add(statePanel);
    	mainPanel.add(aPanel);
    	mainPanel.add(bPanel);
    	mainPanel.add(ePanel);
    	mainPanel.add(fPanel);
    	statePanel.setVisible(true);
    	aPanel.setVisible(true);
    	bPanel.setVisible(true);
    	ePanel.setVisible(true);
    	fPanel.setVisible(true);
    	topPanel.setVisible(true);
    	buttonPanel.add(b_OK);
    	buttonPanel.add(b_restart);
    	
    	add(mainPanel,"Center");
    	add(buttonPanel,"South");
    	setSize(600,700);
    	setVisible(true);
    }
  
public void createGUI(AFN afn){
	
	
	int numStates = afn.size();
	transitionTable = new String[numStates];
	/*
	topPanel = new JPanel();
	statePanel= new JPanel();
	aPanel = new JPanel();
	bPanel = new JPanel();
	ePanel= new JPanel();
	fPanel= new JPanel();
	
	mainPanel = new JPanel();
	setLayout(new BorderLayout());
	add(topPanel,"North");

	
	mainPanel.setLayout(new GridLayout(1,5));
	topPanel.setLayout(new GridLayout(1,4));
	topPanel.add(new JLabel("     State"));
	topPanel.add(new JLabel("    a    "));
	topPanel.add(new JLabel("    b    "));
	topPanel.add(new JLabel("  epsilon "));
	topPanel.add(new JLabel(" accept"));
	*/
	removeAllComponents(mainPanel);
	removeAllComponents(statePanel);
	removeAllComponents(aPanel);
	removeAllComponents(bPanel);
	removeAllComponents(ePanel);
	removeAllComponents(fPanel);
	
	statePanel.setLayout(new GridLayout(numStates,1));
	aPanel.setLayout(new GridLayout(numStates,1));
	bPanel.setLayout(new GridLayout(numStates,1));
	ePanel.setLayout(new GridLayout(numStates,1));
	fPanel.setLayout(new GridLayout(numStates,1));
	
	c_final= new Checkbox[numStates];
	l_state= new JLabel[numStates];
	t_text1= new JTextField[numStates];
	t_text2= new JTextField[numStates];
	t_text3= new JTextField[numStates];
	
	int i = 0;
	for(AFNState state : afn.values()){
		
		if(state.getType()== AFNStateType.START || state.getType()== AFNStateType.START_FINAL)
			l_state[i]= new JLabel(" >>"+state.getStateName());
		else
			l_state[i]= new JLabel(" "+state.getStateName());
		t_text1[i]= new JTextField(""+state.getTransition("a"));
		t_text2[i]= new JTextField(""+state.getTransition("b"));
		t_text3[i]= new JTextField(8);
		
		c_final[i] = new Checkbox();
		if(state.getType()== AFNStateType.FINAL || state.getType()== AFNStateType.START_FINAL)
			c_final[i] = new Checkbox("",true);
		else
			c_final[i] = new Checkbox("");
			
		c_final[i].addItemListener(this);
		//System.out.println("creating and adding Label");
		statePanel.add(l_state[i]);
		aPanel.add(t_text1[i]);
		bPanel.add(t_text2[i]);
		ePanel.add(t_text3[i]);
		fPanel.add(c_final[i]);
		
		//transitionTable[i]="q"+i+" ";
		i++;
		
	}
	
	
	
	mainPanel.add(statePanel);
	mainPanel.add(aPanel);
	mainPanel.add(bPanel);
	mainPanel.add(ePanel);
	mainPanel.add(fPanel);
	statePanel.setVisible(true);
	aPanel.setVisible(true);
	bPanel.setVisible(true);
	ePanel.setVisible(true);
	fPanel.setVisible(true);
	topPanel.setVisible(true);

	//add(mainPanel,"Center");
	//buttonPanel.add(b_OK);
	//buttonPanel.add(b_restart);
	validate();
	//add(buttonPanel,"South");
	
	//setVisible(true);
}

	private void removeAllComponents(JPanel pane){
	
		//int count = pane.getComponentCount();
		Component[] components = pane.getComponents();
		for(int i = 0; i< components.length;i++){
			
			pane.remove(components[i]);
		}
		validate();
	}

    public void actionPerformed(ActionEvent evt){
    	
    	Object src = evt.getSource();
    	if( ((JButton)src).getLabel().equals("MINIMIZE IT!!")){
    		AFN afn;
    		System.out.println("MINIMIZE IT");	
    		 afn = new AFN();
    		AFNState stateList[];//create the statelist
    		stateList = new AFNState[transitionTable.length];
    		for(int i=0; i<transitionTable.length;i++){
    			
    			//create state
    			
    		if(i==0) //initial state
    			if(c_final[0].getState())
    				stateList[0] = new AFNState("q0",AFNStateType.START_FINAL);
    			else
    				stateList[0] = new AFNState("q0",AFNStateType.START);
    				
    		else
    			
    			if(c_final[i].getState())
    			{ //checs for a final state
    				stateList[i]= new AFNState("q"+i,AFNStateType.FINAL);
    				System.out.println("q"+i+" is FINAL");
    			}
    			else
    				stateList[i]= new AFNState("q"+i,AFNStateType.NORMAL);
    			
    			
    			//get strings from table
    			 StringTokenizer st; 
    			 ArrayList<String> list = new ArrayList<String>();
    			
    			String a = t_text1[i].getText();
    			
    			st = new StringTokenizer(a);
    		     while (st.hasMoreTokens()) {
    		        
    		    	 list.add(st.nextToken());
    		    	 //System.out.println("State q"+i+" adding 'a' transition");
    		     }
    			//add "a" transitions
    		     stateList[i].addTransition("a", list);
    			
    			
                String b = t_text2[i].getText();
    			
    			st = new StringTokenizer(b);
    			list = new ArrayList<String>();
    		     while (st.hasMoreTokens()) {
    		        
    		    	 list.add(st.nextToken());
    		    	 //System.out.println("State q"+i+" adding 'b' transition");
    		     }
    		    stateList[i].addTransition("b", list);
    		    
               String e = t_text3[i].getText();
    			
    			st = new StringTokenizer(e);
    			list = new ArrayList<String>();
    		     while (st.hasMoreTokens()) {
    		        
    		    	 list.add(st.nextToken());
    		    	 //System.out.println("State q"+i+" adding 'b' transition");
    		     }
    		    stateList[i].addTransition(AFN.epsilon, list);
    		    
    			//add currentState to the AFN
    			afn.addState(stateList[i]);
    			//System.out.println("adding state q"+i);
    		
    				
    				
    			
    		}
    		
    		AFN afnmin;
    		System.out.println("Start Minimize");
    		afnmin = afn.minimize();
    		System.out.println("finished minimizing");
    		System.out.println("cleanup GUI");
    		
    		createGUI(afnmin);
    		
    		
    	}	
    	if( ((JButton)src).getLabel().equals("Start Over")){
    		
    		initialStates= Integer.parseInt(JOptionPane.showInputDialog(this,"Escribe el numero de estados de tu AFN" ));
        	
        	removeAllComponents(mainPanel);
        	createGUI(initialStates);
    	}
    }
    
    public void itemStateChanged(ItemEvent evt){
    	
    	
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
        JFrame myApp = new GUI();
    }
}

